package com.example.administrator.radarplusfriendsdemo.custom;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.View;

import com.example.administrator.radarplusfriendsdemo.R;
import com.example.administrator.radarplusfriendsdemo.been.EightPointConfig;


/**
 * 雷达图
 */
public class RadarView extends View {

    private Paint mPaintLine;//画圆线需要用到的paint
    private Paint mPaintCircle;//画圆需要用到的paint
    private Paint mPaintScan;//画扫描需要用到的paint

    private int mWidth, mHeight;//整个图形的长度和宽度

    private Matrix matrix = new Matrix();//旋转需要的矩阵
    private int scanAngle;//扫描旋转的角度
    private Shader scanShader;//扫描渲染shader
    private int scanSpeed = 1;//扫描的速度
    private Bitmap centerBitmap;//最中间icon

    //每个圆圈所占的比例
    private static float[] circleProportion =
                    {EightPointConfig.RADIUS_0_RATE,
                    EightPointConfig.RADIUS_1_RATE,
                    EightPointConfig.RADIUS_2_RATE,
                    EightPointConfig.RADIUS_3_RATE,
                    EightPointConfig.RADIUS_4_RATE};

    private Runnable run = new Runnable() {
        @Override
        public void run() {
            //每次执行的时候，让绘制圆的角度旋转一下 + 渐变色，这样就形成了雷达的效果。
            scanAngle = (scanAngle + scanSpeed) % 360;
            matrix.postRotate(scanSpeed, mWidth / 2, mHeight / 2);
            invalidate();
            postDelayed(run, 10);
        }
    };

    public RadarView(Context context) {
        this(context, null);
    }

    public RadarView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RadarView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        post(run);
    }


    private void init() {
        //画线的画笔
        mPaintLine = new Paint();
        mPaintLine.setColor(getResources().getColor(R.color.line_color_blue));
        mPaintLine.setAntiAlias(true);
        mPaintLine.setStrokeWidth(1);
        mPaintLine.setStyle(Paint.Style.STROKE);
        //画圆的画笔
        mPaintCircle = new Paint();
        mPaintCircle.setColor(Color.WHITE);
        mPaintCircle.setAntiAlias(true);
        //画扫描图的画笔
        mPaintScan = new Paint();
        mPaintScan.setStyle(Paint.Style.FILL_AND_STROKE);
        //中间人的头像
        centerBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.circle_photo);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        mWidth = getWidth();
        mHeight = getHeight();
        scanShader = new SweepGradient(mWidth / 2, mHeight / 2,
                new int[]{Color.TRANSPARENT, Color.parseColor("#44FFFFFF")}, null);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawCircle(canvas);
        drawScan(canvas);
        drawCenterIcon(canvas);
    }

    /**
     * 绘制圆线圈
     *
     * @param canvas
     */
    private void drawCircle(Canvas canvas) {
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * 1.0f / 2 * circleProportion[0], mPaintLine);     // 绘制小圆
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * 1.0f / 2 * circleProportion[1], mPaintLine);   // 绘制中圆
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * 1.0f / 2 * circleProportion[2], mPaintLine); // 绘制中大圆
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * 1.0f / 2 * circleProportion[3], mPaintLine);  // 绘制大圆
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * 1.0f / 2 * circleProportion[4], mPaintLine);  // 绘制大大圆
    }

    /**
     * 绘制扫描
     *
     * @param canvas
     */
    private void drawScan(Canvas canvas) {
        canvas.save();
        mPaintScan.setShader(scanShader);
        canvas.concat(matrix);
        canvas.drawCircle(mWidth / 2, mHeight / 2, mWidth * 1.0f / 2 * circleProportion[4], mPaintScan);
        canvas.restore();
    }

    /**
     * 绘制最中间的图标
     *
     * @param canvas
     */
    private void drawCenterIcon(Canvas canvas) {
        canvas.drawBitmap(centerBitmap, null,
                new Rect((int) (mWidth / 2 - mWidth * 1.0 / 2 * circleProportion[0]), (int) (mHeight / 2 - mWidth * 1.0 / 2 * circleProportion[0]),
                        (int) (mWidth / 2 + mWidth * 1.0 / 2 * circleProportion[0]), (int) (mHeight / 2 + mWidth * 1.0 / 2 * circleProportion[0])), mPaintCircle);
    }

}
